package com.compty.security.web;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.compty.core.struts2.BaseAction;
import com.compty.security.domain.Menu;
import com.compty.security.domain.User;


public class SecurityAction extends BaseAction<User>{
	
	private static final long serialVersionUID = -6592966554921061169L;

    public HttpServletResponse getResponse() {
        return ServletActionContext.getResponse();
    }

    public PrintWriter getWriter() throws IOException {
        return ServletActionContext.getResponse().getWriter();
    }

    public List<Menu> filterMenu(Collection<Menu> menus, List<Long> menuIds) {
        List<Menu> menuList = new ArrayList<Menu>();

        for (Menu m : menus) {
            if (menuIds.contains(m.getId())) {
                menuList.add(m);

                List<Menu> children = filterMenu(m.getChildren(), menuIds);
                m.getChildren().clear();
                m.getChildren().addAll(children);
            }
        }

        return menuList;
    }
}
